﻿using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using BusinessLogic.Model;

namespace BusinessLogic
{
    public static class Utilities
    {
        private static int timeDifference = Convert.ToInt32(ConfigurationManager.AppSettings["TimeDifference"]);

        //capture [] tags
        //the content of the opening tag with the attributes is in backreference group 1
        //the tag itself is in backreference 2
        //the value of the tag is in backreference 3
        private const string CaptureBracketTags = @"\[(([A-Z][A-Z0-9]*)\b[^\]]*)](.*?)\[/\2]";
        private const string CaptureHtmlTags = @"<(([A-Z][A-Z0-9]*)\b[^>]*)>(.*?)</\2>";

        /// <summary>
        /// Adjust the time according to the time difference and formats it
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ToTimeAdjustedString(this DateTime date)
        {
            return date.AddHours(timeDifference).ToString(SettingsDefaults.DateTimeFormat);
        }

        /// <summary>
        /// Replaces bracket tags with HTML tags
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string AddHtmlTags(this string arg)
        {
            //Singleline mode needed to make the dot match new line. This way tags that are include new line will be matched.
            Regex regex = new Regex(CaptureBracketTags, RegexOptions.IgnoreCase | RegexOptions.Singleline);

            //the first backreference contains the whole opening tag with attributes
            //the second contains only the name of the tag
            //the third contains the value of the tag
            return regex.Replace(arg, "<$1>$3</$2>");
        }

        /// <summary>
        /// Replaces HTML tags with bracket tags
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static string RemoveHtmlTags(this string arg)
        {
            //Singleline mode needed to make the dot match new line. This way tags that are include new line will be matched.
            Regex regex = new Regex(CaptureHtmlTags, RegexOptions.IgnoreCase | RegexOptions.Singleline);

            //the first backreference contains the whole opening tag with attributes
            //the second contains only the name of the tag
            //the third contains the value of the tag
            return regex.Replace(arg, "[$1]$3[/$2]");
        }

        /// <summary>
        /// Replaces the URLs in the input with HTML links
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ReplaceUrlsWithLinks(this string input)
        {
            return Regex.Replace(input,
                @"\b(((http(s)?|ftp|file)://)|www\.)[^\s]+",
                match =>
                {
                    string url = match.Value.StartsWith("www.", StringComparison.InvariantCultureIgnoreCase) ? "http://" + match.Value : match.Value;
                    return String.Format("<a target='_blank' href='{0}'>{1}</a>", url, match.Value);
                },
                RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
        }

        public static string ReplaceLinksWithUrls(this string input)
        {
            return Regex.Replace(input, @"<a\b[^>]*>(?<URL>.*?)</a>", "${URL}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
        }

        public static string HtmlEncode(this string input)
        {
            return HttpUtility.HtmlEncode(input);
        }

        public static string HtmlDecode(this string input)
        {
            return HttpUtility.HtmlDecode(input);
        }
    }
}
